package vn.com.bookstore.fragment;

import java.util.ArrayList;

import vn.com.bookstore.Config;
import vn.com.bookstore.ConnectActivity;
import vn.com.bookstore.ObjectFragment;
import vn.com.bookstore.R;
import vn.com.bookstore.adapter.StoreAdapter;
import vn.com.bookstore.model.Book;
import vn.com.bookstore.model.ListStore;
import vn.com.bookstore.model.Store;
import vn.com.bookstore.network.NetworkAdapter;
import vn.com.bookstore.network.NetworkAdapter.OnNetworkListener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

public class StoreFragment extends ObjectFragment implements OnItemClickListener, OnNetworkListener {
	public static final String TAG = "StoreActivity";

	private View parent;

	protected View mNoData;

	protected ListView gridStoreBook;

	protected StoreAdapter mStoreAdapter;

	protected LinearLayout mLayoutLoading;

	ArrayList<Book> mData;
	protected ArrayList<Store> stores;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		parent = inflater.inflate(R.layout.fragment_store, null);

		getActivity().setTitle(this.getString(R.string.activity_store_title));
		findViewById();

		stores = new ArrayList<Store>();
		mStoreAdapter = new StoreAdapter(getActivity(), stores);
		gridStoreBook.setAdapter(mStoreAdapter);
		gridStoreBook.setOnItemClickListener(this);

		loadData(false);

		return parent;
	}

	@Override
	public void loadData(final boolean forceLoad) {
		// get list of Store
		new Thread() {
			public void run() {
				setLoading(true);
				NetworkAdapter net = new NetworkAdapter(mApplication, mHandler, StoreFragment.this);
				Config.NetObject obj = Config.BookStore.getUrlListStore(1, Config.LIMIT);
				Log.e("store", "____khac.pvan: " + obj.url);
				net.sendHttpGet(ConnectActivity.METHOD_GET_STORE, obj.url, obj.param, forceLoad);
			};
		}.start();
	}

	public void setLoading(final boolean isLoading) {
		mLayoutLoading.post(new Runnable() {

			@Override
			public void run() {

				mLayoutLoading.setVisibility(View.GONE);
				if (isLoading) {
					mLayoutLoading.setVisibility(View.VISIBLE);
					mNoData.setVisibility(View.GONE);
				} else {
					refreshUI();
				}
			}
		});

	}

	@Override
	protected void findViewById() {
		gridStoreBook = (ListView) parent.findViewById(R.id.grid_book);
		mLayoutLoading = (LinearLayout) parent.findViewById(R.id.layout_loading);
		mNoData = parent.findViewById(R.id.tv_nodata);
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View itemStore, int position, long itemID) {
		Store store = stores.get(position);
		OnStoreFragmentListener listener = (OnStoreFragmentListener) this.getActivity();
		if (listener != null) {
			listener.onStoreClicked(adapter, itemStore, position, store);
		}
	}

	@Override
	public void onResponse(int idMethod, boolean isSuccess, String json) {
		Log.e(TAG, "result: " + isSuccess + " json: " + json);
		setLoading(false);

		switch (idMethod) {
		case ConnectActivity.METHOD_GET_STORE: // store
			stores = new ArrayList<Store>(ListStore.parseListGson(json));
			mStoreAdapter.setData(stores);

			break;
		default:
			break;
		}
		refreshUI();
	}

	@Override
	public void refreshUI() {
		if (mStoreAdapter.getCount() == 0) {
			gridStoreBook.setVisibility(View.GONE);
			mNoData.setVisibility(View.VISIBLE);
		} else {
			gridStoreBook.setVisibility(View.VISIBLE);
			mNoData.setVisibility(View.GONE);
		}
	}

	public interface OnStoreFragmentListener {
		public void onStoreClicked(AdapterView<?> adapter, View itemStore, int position, Store store);
	}
}
